<?php
namespace User\Controller;

use Common\Api\Wechat;
use Think\Controller;
use Common\Controller\WeixinController;

class AccreditController extends WeixinController
{
    protected $weobj;//实例化微信对象

    public function _initialize()
    {
        //获取公众号配置文件
        parent::_initialize();
        $this->weObj = new Wechat($this->config);

    }

    //用户授权账号绑定
    public function bind($scope = 'snsapi_base')
    {
        $callback = "http://" . $_SERVER["SERVER_NAME"] . "/User/Accredit/getuserinfo";
        $state = 'STATE';
        $wap_url = $this->weObj->getOauthRedirect($callback, $state, $scope);
        header("location:$wap_url");

    }

    //获取用户信息
    public function getuserinfo()
    {

        $data = $this->weObj->getOauthAccessToken($_GET);
        $openid = $data['openid'];
        $access_token = $data['access_token'];
        $userinfo = $this->weObj->getOauthUserinfo($access_token, $openid);

        if ($userinfo == false || $userinfo['errcode'] == 41008) {
            header("location:http://" . $_SERVER["HTTP_HOST"] . "/User/Accredit/bind/scope/snsapi_userinfo");
        } else {

            $user_result = M('Member')->where("open_id='%s' and status=%d", [$openid, 1])->find();
            if ($user_result) {
                cookie('user_openid', $openid, array('expire' => 3600 * 3600 * 24));
                cookie('user_userId', $user_result['id'], array('expire' => 3600 * 3600 * 24));
                $this->redirect('Index/Index');
                exit;
            }
            if ($userinfo) {
                $user_data = $userinfo;
                S('info', $userinfo, 200);
            } else {
                $user_data = S('info');
            }
            if (empty($user_data)) {
                $this->error('数据失效,请重新登录', U('Accredit/bind'));
                exit;
            }
            $this->assign('title', '账号绑定');
            $this->assign('userinfo', $user_data);
            $this->display();


        }

    }

    //提交账号绑定
    public function getBind()
    {
        if (IS_POST) {

            $username = $_POST['username'];
            $pwd=$_POST['password'];
            if(empty($username) || empty($pwd)){
                $this->error('请输入账号密码');
                exit;
            }
            $password = md5($pwd);
            $uid = M("Member")->where("username='%s' and password='%s'", [$username, $password])->getField('id');
            if ($uid) {
                $data['open_id'] = $_POST['open_id'];
                $data['headimgurl'] = $_POST['headimgurl'];
                if (M('Member')->where("id=%d", $uid)->save($data)) {
                    cookie('user_openid', $_POST['open_id'], array('expire' => 3600 * 3600 * 24));
                    cookie('user_userId', $uid, array('expire' => 3600 * 3600 * 24));
                    $this->success('绑定成功',U('Index/index'));
                } else {
                    $this->error('绑定失败');
                }
            }else{
                $this->error('账号或密码错误');
            }
        }


    }

}